﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using System.Globalization;

namespace DentalSoftware
{
    public partial class PhieuThu : Form
    {
        private double soTien;

        public double SoTien
        {
            get { return soTien; }
            set { soTien = value; }
        }

        private int isMultiMoney;

        public PhieuThu()
        {
            InitializeComponent();
            txtMaPhieu.Text = ThuChiBUS.GetMaxMa().ToString();
            isMultiMoney = 0;
        }
        
        public PhieuThu(DateTime ngay, string nth, string dc, string dt, string nd)
        {
            InitializeComponent();

            txtDiaChi.Text = dc;
            mtbDienThoai.Text = dt;
            txtNguoiNopTien.Text = nth;
            dtpNgay.Value = ngay;
            txtNoiDung.Text = nd;
            txtMaPhieu.Text = ThuChiBUS.GetMaxMa().ToString();

            txtNoiDung.Enabled = false;
            txtNguoiNopTien.Enabled = false;
            dtpNgay.Enabled = false;
            mtbDienThoai.Enabled = false;
            txtDiaChi.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (isMultiMoney == 0)
                txtSoTien_Leave(sender, e);
            PhieuThuChiDTO ptc = new PhieuThuChiDTO();
            ptc.Ngay = new DateTime(dtpNgay.Value.Year, dtpNgay.Value.Month, dtpNgay.Value.Day);
            ptc.Nguoithuchien = txtNguoiNopTien.Text;
            ptc.Diachi = txtDiaChi.Text;
            ptc.Dienthoai = mtbDienThoai.Text;
            ptc.Noidung = txtNoiDung.Text;
            ptc.Sotien = soTien;
            ptc.Loai = 1;

            ThuChiBUS.InsertPhieu(ptc);

            this.DialogResult = DialogResult.Yes;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void txtSoTien_Leave(object sender, EventArgs e)
        {
            if (IsNumber(txtSoTien.Text))
            {
                soTien = double.Parse(txtSoTien.Text) * 1000; ;
                txtSoTien.Text = soTien.ToString("#,#", CultureInfo.InvariantCulture);
                isMultiMoney = 1;
            }
            else
            {
                MessageBox.Show("Bạn phải nhập số vào ô số tiền!", "Thông báo", MessageBoxButtons.OK);
                txtSoTien.Focus();
                txtSoTien.SelectAll();
            }
        }

        public bool IsNumber(string pValue)
        {
            foreach (Char c in pValue)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }        
        
    }
}
